package ro.flashbite.loaders.types
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import ro.flashbite.helpers.MovieClipHelpers;
	
	/**
	 * This will load a swf file // or a picture(jpg, png or bmp)
	 *  
	 * @author Adrian Barbu
	 * 
	 */
	public final class SwfAsset extends Asset
	{
		// used to make a copy pixels from this bmp.bitmpData to slow loading but accelerate init time;
		private static const BLIT_BMP_DATA	:	BitmapData = new BitmapData(1, 1);
		private static const BLIT_POINT		:	Point = new Point();
		private static const BLIT_RECT		:	Rectangle = new Rectangle(0, 0, 1, 1);
		
		/**
		 * string for logger 
		 */		
		private static const NAME	:	String 	= "SwfAsset";
		/**
		 * publish dimensions of the loaded swf 
		 */		
		private var 	_realWidth	:	uint;
		private var 	_realHeight	:	uint;
		
		//==================================================================================================
		// CONSTRUCTOR
		//==================================================================================================
		
		public function SwfAsset() { super(); }
		
		//==================================================================================================
		// DESTRUCTOR
		//==================================================================================================
		
		override public function destroy():void
		{
			super.destroy();
			
			BLIT_BMP_DATA.dispose();
		}
		
		//==================================================================================================
		// PROTECTED OVERRIDES
		//==================================================================================================
		
		override protected function setLoggerName():void
		{
			_loggerName = NAME;
		}
		
		override protected function setLoaderAndDispacher():void
		{
			_loader = new Loader();
			_loaderDispacher = _loader.contentLoaderInfo;
		}
		
		override protected function onComplete(e:Event):void
		{
			if (_loaderDispacher && _loader) {
				_content = _loader.content;
				try {
					if (_content is MovieClip) {
						MovieClipHelpers.gotoAndStopRecursive(_content);
					} else if (_content is Bitmap && _id && _id.indexOf("sheet") > -1) { //if is a sheet, optimize by performing first copyPixels() operation
						BLIT_BMP_DATA.copyPixels(_content.bitmapData, BLIT_RECT, BLIT_POINT);
					}
				} catch (e:Error) { /*_logger.error("Trying to reset all in loaded movieclip to frame 1");*/ }
				_appDom = (_loaderDispacher as LoaderInfo).applicationDomain;
				
				try {
					_realWidth = _loader.contentLoaderInfo.width;
					_realHeight = _loader.contentLoaderInfo.height;
				} catch (e:Error) {_logger.error("Unable to set real Width and Height"); }
					
				removeListenersFromLoader(_loaderDispacher);
			}	
			
			super.onComplete(e);
		}

		//==================================================================================================
		// GETTERS
		//==================================================================================================
		
		public function get realWidth()	: uint { return _realWidth; 	}
		public function get realHeight(): uint { return _realHeight;	}
	}
}